package com.ruoyi.standingbook.greenery.service.impl;

import cn.hutool.core.bean.BeanUtil;
import com.ruoyi.common.utils.BeanCopyUtils;
import com.ruoyi.common.utils.StringUtils;
import com.ruoyi.common.core.page.TableDataInfo;
import com.ruoyi.common.core.domain.PageQuery;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.ruoyi.standingbook.cleaning.domain.CleaningImportDetail;
import com.ruoyi.standingbook.cleaning.domain.vo.CleaningImportDetailVo;
import lombok.RequiredArgsConstructor;
import org.springframework.stereotype.Service;
import com.ruoyi.standingbook.greenery.domain.bo.GreeneryImportDetailBo;
import com.ruoyi.standingbook.greenery.domain.vo.GreeneryImportDetailVo;
import com.ruoyi.standingbook.greenery.domain.GreeneryImportDetail;
import com.ruoyi.standingbook.greenery.mapper.GreeneryImportDetailMapper;
import com.ruoyi.standingbook.greenery.service.IGreeneryImportDetailService;

import java.util.List;
import java.util.Map;
import java.util.Collection;

/**
 * 道路绿化管养导入明细Service业务层处理
 *
 * @author cc
 * @date 2023-08-29
 */
@RequiredArgsConstructor
@Service
public class GreeneryImportDetailServiceImpl implements IGreeneryImportDetailService {

    private final GreeneryImportDetailMapper baseMapper;

    /**
     * 查询道路绿化管养导入明细
     */
    @Override
    public GreeneryImportDetailVo queryById(Long id){
        return baseMapper.selectVoById(id);
    }

    /**
     * 查询道路绿化管养导入明细列表
     */
    @Override
    public TableDataInfo<GreeneryImportDetailVo> queryPageList(GreeneryImportDetailBo bo, PageQuery pageQuery) {
        LambdaQueryWrapper<GreeneryImportDetail> lqw = buildQueryWrapper(bo);
        Page<GreeneryImportDetailVo> result = baseMapper.selectVoPage(pageQuery.build(), lqw);
        return TableDataInfo.build(result);
    }

    /**
     * 查询道路绿化管养导入明细列表
     */
    @Override
    public List<GreeneryImportDetailVo> queryList(GreeneryImportDetailBo bo) {
        LambdaQueryWrapper<GreeneryImportDetail> lqw = buildQueryWrapper(bo);
        return baseMapper.selectVoList(lqw);
    }

    private LambdaQueryWrapper<GreeneryImportDetail> buildQueryWrapper(GreeneryImportDetailBo bo) {
        Map<String, Object> params = bo.getParams();
        LambdaQueryWrapper<GreeneryImportDetail> lqw = Wrappers.lambdaQuery();
        lqw.like(StringUtils.isNotBlank(bo.getItemName()), GreeneryImportDetail::getItemName, bo.getItemName());
        lqw.eq(bo.getServiceType()!=null, GreeneryImportDetail::getServiceType, bo.getServiceType());
        lqw.eq(StringUtils.isNotBlank(bo.getStreet()), GreeneryImportDetail::getStreet, bo.getStreet());
        lqw.eq(StringUtils.isNotBlank(bo.getCommunity()), GreeneryImportDetail::getCommunity, bo.getCommunity());
        lqw.eq(StringUtils.isNotBlank(bo.getEndpoint()), GreeneryImportDetail::getEndpoint, bo.getEndpoint());
        lqw.eq(bo.getArea() != null, GreeneryImportDetail::getArea, bo.getArea());
        lqw.eq(StringUtils.isNotBlank(bo.getLevel()), GreeneryImportDetail::getLevel, bo.getLevel());
        lqw.eq(bo.getDate() != null, GreeneryImportDetail::getDate, bo.getDate());
        lqw.eq(bo.getFrequency() != null, GreeneryImportDetail::getFrequency, bo.getFrequency());
        lqw.eq(bo.getActualFrequency() != null, GreeneryImportDetail::getActualFrequency, bo.getActualFrequency());
        lqw.eq(StringUtils.isNotBlank(bo.getStatus()), GreeneryImportDetail::getStatus, bo.getStatus());
        lqw.eq(StringUtils.isNotBlank(bo.getPic1()), GreeneryImportDetail::getPic1, bo.getPic1());
        lqw.eq(StringUtils.isNotBlank(bo.getPic2()), GreeneryImportDetail::getPic2, bo.getPic2());
        lqw.eq(StringUtils.isNotBlank(bo.getPic3()), GreeneryImportDetail::getPic3, bo.getPic3());
        lqw.eq(StringUtils.isNotBlank(bo.getPic4()), GreeneryImportDetail::getPic4, bo.getPic4());
        lqw.eq(StringUtils.isNotBlank(bo.getProviderId()), GreeneryImportDetail::getProviderId, bo.getProviderId());
        lqw.eq(StringUtils.isNotBlank(bo.getBatchNo()), GreeneryImportDetail::getBatchNo, bo.getBatchNo());
        lqw.eq(bo.getProcessStatus() != null, GreeneryImportDetail::getProcessStatus, bo.getProcessStatus());
        lqw.eq(StringUtils.isNotBlank(bo.getProcessMsg()), GreeneryImportDetail::getProcessMsg, bo.getProcessMsg());
        return lqw;
    }

    /**
     * 新增道路绿化管养导入明细
     */
    @Override
    public Boolean insertByBo(GreeneryImportDetailBo bo) {
        GreeneryImportDetail add = BeanUtil.toBean(bo, GreeneryImportDetail.class);
        validEntityBeforeSave(add);
        boolean flag = baseMapper.insert(add) > 0;
        if (flag) {
            bo.setId(add.getId());
        }
        return flag;
    }

    /**
     * 修改道路绿化管养导入明细
     */
    @Override
    public Boolean updateByBo(GreeneryImportDetailBo bo) {
        GreeneryImportDetail update = BeanUtil.toBean(bo, GreeneryImportDetail.class);
        validEntityBeforeSave(update);
        return baseMapper.updateById(update) > 0;
    }

    /**
     * 保存前的数据校验
     */
    private void validEntityBeforeSave(GreeneryImportDetail entity){
        //TODO 做一些数据校验,如唯一约束
    }

    /**
     * 批量删除道路绿化管养导入明细
     */
    @Override
    public Boolean deleteWithValidByIds(Collection<Long> ids, Boolean isValid) {
        if(isValid){
            //TODO 做一些业务上的校验,判断是否需要校验
        }
        return baseMapper.deleteBatchIds(ids) > 0;
    }

    @Override
    public Boolean batchInsertVo(List<GreeneryImportDetailVo> voList) {
        List<GreeneryImportDetail> list= BeanCopyUtils.copyList(voList , GreeneryImportDetail.class);
        return baseMapper.insertBatch(list);
    }
}
